﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.LuFang.API.Dtos;
using RBAC.LuFang.Domain;
using RBAC.LuFang.Domain.DTO;
using RBAC.LuFang.Domin.Services;
using System.Collections.Generic;
using System.Security.Claims;

namespace RBAC.LuFang.API.Controllers
{
    /// <summary>
    /// 请假控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class LeaveapplicationController : ControllerBase
    {
        private readonly ILeaveapplicationService _leaveService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="leaveService"></param>
        public LeaveapplicationController(ILeaveapplicationService leaveService)
        {
            _leaveService = leaveService;
        }
        /// <summary>
        /// 请假添加
        /// </summary>
        /// <param name="leaveapplication"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddLeaveapplication(Leaveapplication leaveapplication)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data=await _leaveService.AddLeaveapplication(leaveapplication);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 获取用户反填请假
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<GetLeaveapplicationFanJWTDTO>>> GetLeaveaFanJWT()
        {
            ApiResult<List<GetLeaveapplicationFanJWTDTO>> result=new ApiResult<List<GetLeaveapplicationFanJWTDTO>>();
            try
            {
                var id = Convert.ToInt32(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
                result.Data=await _leaveService.GetLeaveapplicationFanJWT(id);
            }
            catch (Exception ee)
            {
                result.Code= -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 请假列表
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<LeavPage>> GetLeaveapplication1(int pageindex, int pagesize, string? kdate, string? sdate, string? state, string? leavname)
        {
            ApiResult<LeavPage> result=new ApiResult<LeavPage>();
            try
            {
                var uid = Convert.ToInt32(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
                result.Data=await _leaveService.GetLeaveapplication(uid, pageindex,pagesize,kdate,sdate,state,leavname);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除请假
        /// </summary>
        /// <param name="leavid"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelLeaveapplication(int leavid)
        {
            ApiResult<int> result=new ApiResult<int>();
            try
            {
                result.Data= await _leaveService.DelLeaveapplication(leavid);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="leavid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Leaveapplication>> FanLeaveapplication(int leavid)
        {
            ApiResult<Leaveapplication> result=new ApiResult<Leaveapplication>();
            try
            {
                result.Data= await _leaveService.FanLeaveapplication(leavid);
            }
            catch (Exception ee)
            {
                result.Code= -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改审批
        /// </summary>
        /// <param name="leavid"></param>
        /// <param name="State"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdShenPiLeaveapplication(int leavid, string State)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data= await _leaveService.UpdShenPiLeaveapplication(leavid, State);
            }
            catch (Exception ee)
            {
                result.Code=-1;
                result.Message= ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 请假修改
        /// </summary>
        /// <param name="leavid"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> Upd(Leaveapplication leaveapplication)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _leaveService.Upd(leaveapplication);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }





    }
}
